//
//  KDAddNumberView.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/5.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDAddNumberView.h"

@implementation KDAddNumberView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _morningInt = 0;
        _afternoonInt = 0;
        _eveningInt = 0;
        
        [self createUI];
        
    }
    return self;
}

- (void)createUI
{
    self.morningLabel = [[UILabel alloc] initWithFrame:(CGRect){(kScreenWidth-517/2)/2,10,517/2,54/2}];
    [_morningLabel setTextAlignment:NSTextAlignmentCenter];
    [_morningLabel setFont:[UIFont systemFontOfSize:12]];
    [_morningLabel setBackgroundColor:[UIColor colorWithHexString:@"#f0f0f0"]];
    [_morningLabel setTextColor:[UIColor colorWithHexString:@"#222222"]];
    [_morningLabel setText:@"上午门诊"];
    [self addSubview:_morningLabel];
    
    self.morningPeopleLabel = [[UILabel alloc] initWithFrame:(CGRect){235,Y(_morningLabel)+HEIGHT(_morningLabel)+27/2+14/2,18,12}];
    [_morningPeopleLabel setTextAlignment:NSTextAlignmentCenter];
    [_morningPeopleLabel setFont:[UIFont systemFontOfSize:12]];
    [_morningPeopleLabel setBackgroundColor:[UIColor clearColor]];
    [_morningPeopleLabel setTextColor:[UIColor blackColor]];
    [_morningPeopleLabel setText:@"0"];
    [self addSubview:_morningPeopleLabel];
    
    self.afternoonLabel = [[UILabel alloc] initWithFrame:(CGRect){X(_morningLabel),Y(_morningPeopleLabel)+HEIGHT(_morningPeopleLabel)+38/2,517/2,54/2}];
    [_afternoonLabel setTextAlignment:NSTextAlignmentCenter];
    [_afternoonLabel setFont:[UIFont systemFontOfSize:12]];
    [_afternoonLabel setBackgroundColor:[UIColor colorWithHexString:@"#f0f0f0"]];
    [_afternoonLabel setTextColor:[UIColor colorWithHexString:@"#222222"]];
    [_afternoonLabel setText:@"下午门诊"];
    [self addSubview:_afternoonLabel];
    
    self.afternoonPeopleLabel = [[UILabel alloc] initWithFrame:(CGRect){X(_morningPeopleLabel),Y(_afternoonLabel)+HEIGHT(_afternoonLabel)+27/2+14/2,WIDTH(_morningPeopleLabel),HEIGHT(_morningPeopleLabel)}];
    [_afternoonPeopleLabel setTextAlignment:NSTextAlignmentCenter];
    [_afternoonPeopleLabel setFont:[UIFont systemFontOfSize:12]];
    [_afternoonPeopleLabel setBackgroundColor:[UIColor clearColor]];
    [_afternoonPeopleLabel setTextColor:[UIColor blackColor]];
    [_afternoonPeopleLabel setText:@"0"];
    [self addSubview:_afternoonPeopleLabel];
    
    self.eveningLabel = [[UILabel alloc] initWithFrame:(CGRect){X(_morningLabel),Y(_afternoonPeopleLabel)+HEIGHT(_afternoonPeopleLabel)+38/2,517/2,54/2}];
    [_eveningLabel setTextAlignment:NSTextAlignmentCenter];
    [_eveningLabel setFont:[UIFont systemFontOfSize:12]];
    [_eveningLabel setBackgroundColor:[UIColor colorWithHexString:@"#f0f0f0"]];
    [_eveningLabel setTextColor:[UIColor colorWithHexString:@"#222222"]];
    [_eveningLabel setText:@"晚上门诊"];
    [self addSubview:_eveningLabel];
    
    self.eveningPeopleLabel = [[UILabel alloc] initWithFrame:(CGRect){X(_morningPeopleLabel),Y(_eveningLabel)+HEIGHT(_eveningLabel)+27/2+14/2,WIDTH(_morningPeopleLabel),HEIGHT(_morningPeopleLabel)}];
    [_eveningPeopleLabel setTextAlignment:NSTextAlignmentCenter];
    [_eveningPeopleLabel setFont:[UIFont systemFontOfSize:12]];
    [_eveningPeopleLabel setBackgroundColor:[UIColor clearColor]];
    [_eveningPeopleLabel setTextColor:[UIColor blackColor]];
    [_eveningPeopleLabel setText:@"0"];
    [self addSubview:_eveningPeopleLabel];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:(CGRect){X(_morningLabel),Y(_morningPeopleLabel),100,12}];
    [label1 setTextAlignment:NSTextAlignmentLeft];
    [label1 setFont:[UIFont systemFontOfSize:12]];
    [label1 setBackgroundColor:[UIColor clearColor]];
    [label1 setTextColor:[UIColor blackColor]];
    [label1 setText:@"加号人数"];
    [self addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:(CGRect){X(_morningLabel),Y(_afternoonPeopleLabel),100,12}];
    [label2 setTextAlignment:NSTextAlignmentLeft];
    [label2 setFont:[UIFont systemFontOfSize:12]];
    [label2 setBackgroundColor:[UIColor clearColor]];
    [label2 setTextColor:[UIColor blackColor]];
    [label2 setText:@"加号人数"];
    [self addSubview:label2];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:(CGRect){X(_morningLabel),Y(_eveningPeopleLabel),100,12}];
    [label3 setTextAlignment:NSTextAlignmentLeft];
    [label3 setFont:[UIFont systemFontOfSize:12]];
    [label3 setBackgroundColor:[UIColor clearColor]];
    [label3 setTextColor:[UIColor blackColor]];
    [label3 setText:@"加号人数"];
    [self addSubview:label3];
    
    [self createSixButton];
}

- (void)createSixButton
{
    float   ButtonWidth = 24;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:(CGRect){(kScreenWidth-30-ButtonWidth*2-26/2-28/2-WIDTH(_morningPeopleLabel)),Y(_morningPeopleLabel)-7,ButtonWidth,ButtonWidth}];
    [button setImage:[UIImage imageNamed:@"btn_less.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    button.tag = 101;
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setFrame:(CGRect){(kScreenWidth-30-ButtonWidth),Y(_morningPeopleLabel)-7,ButtonWidth,ButtonWidth}];
    [button2 setImage:[UIImage imageNamed:@"btn_more.png"] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button2];
    button2.tag = 102;

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button3 setFrame:(CGRect){(kScreenWidth-30-ButtonWidth*2-26/2-28/2-WIDTH(_morningPeopleLabel)),Y(_afternoonPeopleLabel)-7,ButtonWidth,ButtonWidth}];
    [button3 setImage:[UIImage imageNamed:@"btn_less.png"] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button3];
    button3.tag = 103;

    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button4 setFrame:(CGRect){(kScreenWidth-30-ButtonWidth),Y(_afternoonPeopleLabel)-7,ButtonWidth,ButtonWidth}];
    [button4 setImage:[UIImage imageNamed:@"btn_more.png"] forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button4];
    button4.tag = 104;
    
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button5 setFrame:(CGRect){(kScreenWidth-30-ButtonWidth*2-26/2-28/2-WIDTH(_morningPeopleLabel)),Y(_eveningPeopleLabel)-7,ButtonWidth,ButtonWidth}];
    [button5 setImage:[UIImage imageNamed:@"btn_less.png"] forState:UIControlStateNormal];
    [button5 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button5];
    button5.tag = 105;
    
    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button6 setFrame:(CGRect){(kScreenWidth-30-ButtonWidth),Y(_eveningPeopleLabel)-7,ButtonWidth,ButtonWidth}];
    [button6 setImage:[UIImage imageNamed:@"btn_more.png"] forState:UIControlStateNormal];
    [button6 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button6];
    button6.tag = 106;
    
    UIButton *button7 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button7 setFrame:(CGRect){X(_morningLabel),Y(button6)+HEIGHT(button6)+13,WIDTH(_morningLabel),35}];
    [button7 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [button7 setBackgroundColor:kNavBlueColor];
    [button7.titleLabel setTextColor:[UIColor whiteColor]];
    [button7 setTitle:@"确  定" forState:UIControlStateNormal];
    [self addSubview:button7];
    button7.tag = 107;
}

- (void)buttonAction1:(UIButton *)button
{
    switch (button.tag) {
        case 101:{
            NSInteger   teger = 0;
            teger = [_morningPeopleLabel.text integerValue];
            if (teger - 1 < 0) {
                //
            }else{
                [_morningPeopleLabel setText:[NSString stringWithFormat:@"%ld",teger-1]];
            }
        }
            break;
        case 102:{
            if ([_morningPeopleLabel.text intValue] == _morningInt) {
                //
                [self showAlert:@"已达到最大加号数"];
            }else{
            [_morningPeopleLabel setText:[NSString stringWithFormat:@"%ld",[_morningPeopleLabel.text integerValue]+1]];
            }
        }break;
        case 103:{
            NSInteger   teger = 0;
            teger = [_afternoonPeopleLabel.text integerValue];
            if (teger - 1 < 0) {
                //
            }else{
                [_afternoonPeopleLabel setText:[NSString stringWithFormat:@"%ld",teger-1]];
            }
        }
            
            break;
        case 104:{
            if ([_afternoonPeopleLabel.text intValue] == _afternoonInt) {
                [self showAlert:@"已达到最大加号数"];
            }else{
            [_afternoonPeopleLabel setText:[NSString stringWithFormat:@"%ld",[_afternoonPeopleLabel.text integerValue]+1]];
            }
        }break;
        case 105:{
            NSInteger   teger = 0;
            teger = [_eveningPeopleLabel.text integerValue];
            if (teger - 1 < 0) {
                //
            }else{
                [_eveningPeopleLabel setText:[NSString stringWithFormat:@"%ld",teger-1]];
            }
        }
            
            break;
        case 106:{
            if ([_eveningPeopleLabel.text intValue] == _eveningInt) {
                [self showAlert:@"已达到最大加号数"];
            }else{
            [_eveningPeopleLabel setText:[NSString stringWithFormat:@"%ld",[_eveningPeopleLabel.text integerValue]+1]];
            }
        }
            break;
            
        case 107:{
            if ([self.addNumberDelegate respondsToSelector:@selector(chooseResult:)]) {
                NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:_morningPeopleLabel.text,@"0",_afternoonPeopleLabel.text,@"1",_eveningPeopleLabel.text,@"2", nil];
                [self.addNumberDelegate chooseResult:dic1];
            }
        }            break;
        default:
            break;
    }
}

- (void)showAlert:(NSString *)str
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

- (void)refeshData:(NSArray *)dataArr
{
    if (dataArr && dataArr.count > 0) {
        for (NSDictionary    *dic1 in dataArr) {
            if ([dic1[@"open_time"] isEqualToString:@"上午"]) {
                _morningInt = [dic1[@"open_num"] intValue];
            }else if ([dic1[@"open_time"] isEqualToString:@"下午"]) {
                _afternoonInt = [dic1[@"open_num"] intValue];
            }else{
                _eveningInt = [dic1[@"open_num"] intValue];
            }
        }
    }
}
@end
